<template>
<div id="UserPage">
    <a-layout id="components-layout-demo-top" class="layout">
      <a-layout-header>
        <div class="logo"/>
        <div style="color:#FFF text-align:right">欢迎您:{{username}} 
        <el-button type="info" @click="logout">安全退出</el-button>
        </div>
      </a-layout-header>
      <a-layout-content :style="{ padding: '0 50px' }">
        <!--修改密码-->
        <div
            :style="{
            background: '#fff',
            padding: '24px',
            marginTop: '20px',
            minHeight: '280px',
          }"
        >
          <p>修改密码</p>
          <a-form-model
              :model="form"
              :label-col="labelCol"
              :wrapper-col="wrapperCol"
          >
            <a-form-model-item label="原密码">
              <a-input type="password" v-model="form.password" />
            </a-form-model-item>

            <a-form-model-item label="新密码">
              <a-input type="password" v-model="form.newpassword" />
            </a-form-model-item>

            <a-form-model-item label="确认新密码">
              <a-input type="password" v-model="form.newpassword1"/>
            </a-form-model-item>

            <a-form-model-item :wrapper-col="{ span: 14, offset: 4 }">
              <a-button
                  type="primary"  @click="ChangePassword"
              >
                提交
              </a-button>
            </a-form-model-item>
          </a-form-model>
        </div>
       </a-layout-content>
    </a-layout>
  </div>

</template>


<script>
export default {
  data() {
    return {
      fileList: [],
      uploading: false,

      wzyfile: null,

      labelCol: {span: 4},
      wrapperCol: {span: 14},
      username:JSON.parse( localStorage.getItem("uname")),
      form: {
        
          password: "",
          newpassword: "",
          newpassword1:"",
      },
    };
  },

  methods:{
			logout(){
				window.localStorage.clear();//清除之前session存的user信息
				this.$router.push("/");//回到首页
			},
      
      async ChangePassword() {
        if (this.form.newpassword == "") {
        this.$vs.notification({text: "密码不能为空"});
        return;
      }
        if(this.form.newpassword!= this.form.newpassword1) {
          this.$vs.notification({ text: "两次输入密码不一致"});
          return;
        } 
        if(this.form.newpassword == this.form.password) {
          this.$vs.notification({ text: "新密码不得与原密码相同"});
          return;
      }   
       const data1 = new FormData()
      data1.append("userName",  localStorage.getItem("uname"))
      data1.append("password", this.form.password)
      let res1 = await this.$http.post("api/2backcode/user/login", data1);
      if (res1.data1 == false) {
          this.$vs.notification({ text: "原密码输入错误"});
          return;
      }   
      const data = new FormData()
      data.append("id", localStorage.getItem("email"))
      data.append("key", "password")
      data.append("value", this.form.newpassword)
      let res = await this.$http.put("api/2backcode/user/update", data);
      console.log(res.data)
      if (res.data != false) {
        this.$vs.notification({
          title: "密码修改成功,请重新登录",
        },
        this.$router.push("/"));
        
      }
    }
      },
   

}




 </script> 


<style>
#UserPage {
  height: 100%;
}

.clearfix {
  padding-left: 155px;
  padding-bottom: 10px;
}

#sc {
  padding-left: 155px;
}
</style>
